package cn.myschool.service;

import cn.myschool.dao.StudentDao;
import cn.myschool.entity.Student;
import cn.myschool.pojo.ExamResult2;
import cn.myschool.pojo.Page;
import cn.myschool.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class StudentService {
    public static final int PAGE_SIZE=20;
    public static final StudentService ME=new StudentService();
//    public Student get(long stuno){
//
//    }
//
//    public int add(Student st);
//
//
//
//    //根据学号修改信息
//    int update(Student student);
//    //根据学号删除学生
//    int del(long id);

    //多条件搜索，返回Page
    public Page<Student> search(String wechat, String qq, String sname, Date start, Date end, int pageNo) {
        SqlSession sqlSession = MybatisUtil.get();
        try {
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            List<Student> list = mapper.search(wechat, qq, sname, start, end, pageNo, PAGE_SIZE);
            int total=mapper.searchTotal(wechat, qq, sname, start, end);
            return new Page<>(pageNo,PAGE_SIZE,total,list);
        } finally {
            MybatisUtil.close(sqlSession);
        }


    }

    //按照 微信号,qq号,姓名,模糊查找学生的功能，加上分页
    public List<Student> search(String wechat, String qq, String sname, Date start, Date end, int pageNo, int pageSize) {
        SqlSession sqlSession = MybatisUtil.get();
        try {
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            List<Student> list = mapper.search(wechat, qq, sname, start, end, pageNo, pageSize);

            return list;
        } finally {
            MybatisUtil.close(sqlSession);
        }


    }

    public int searchTotal( String wechat,  String qq,  String sname,  Date start,  Date end) {
        SqlSession sqlSession = MybatisUtil.get();
        try {
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            return mapper.searchTotal(wechat, qq, sname, start, end);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }


    public  Page<ExamResult2>  findScore(long stuno, int pageNo) {
        pageNo=pageNo<1?1:pageNo;
        SqlSession sqlSession = MybatisUtil.get();
        try {
            int size=2;
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            //return mapper.findScore(stuno, pageNo,PAGE_SIZE);
            List<ExamResult2> list = mapper.findScore(stuno, pageNo, size);
            int scoreTotal = mapper.findScoreTotal(stuno);
            return new Page<ExamResult2>(pageNo,size,scoreTotal,list);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    public Student get(long stuno) {
        SqlSession sqlSession = MybatisUtil.get();
        try {
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);

            return mapper.get(stuno);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }
}
